var userInputpwd = "";
var userType = 0;
var apihost = "";

//background animation
let tl = anime.timeline({
  easing: "easeOutExpo",
  duration: 850,
});

tl.add({
  targets: "section .item",
  width: "100%",
  backgroundColor: "#005CAF",
  delay: anime.stagger(100),
});

tl.add({
  targets: "section .item",
  delay: anime.stagger(70),
  width: "97%",
  backgroundColor: "#8cb5eb",
});

tl.add({
  targets: "section .item",
  backgroundColor: "#FFFFFF",
  delay: anime.stagger(50, { from: "center" }),
});

tl.add(
  {
    targets: "p",
    top: "49%",
    duration: 4500,
    opacity: 1,
  },
  "-=1000"
);

//text animation

//wrap every letter in a span
var textWrapper = document.querySelector(".effect1");
textWrapper.innerHTML = textWrapper.textContent.replace(
  /([^.\s]|\w)/g,
  "<span class='letter'>$&</span>"
);

anime.timeline().add(
  {
    targets: ".effect1 .letter",
    scale: [5, 1],
    opacity: [0, 1],
    translateZ: 0,
    easing: "easeOutExpo",
    duration: 1350,
    delay: function (el, i) {
      return 70 * i;
    },
  },
  1500
);

// 检查网络
setTimeout(function () {
  $(".maindiv").fadeOut(1500, function () {
    $(".scenebox").fadeIn();
  });

  // 绑定双击载入密码框
  $(".doubleTapare").doubleTap(function () {
    // 去掉密码错误提示，清空现有的密码显示
    $(".pwddiv div").removeClass("pwddivErr");
    userInputpwd = "";
    setPwdShow();
    $("#checkPwdModal").modal("show");
  });

  // 检测版本更新
  checkUpdate();

  // 网络检测
  checkNet();
}, 6000);

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
}

// 版本更新检测（检测和安装升级组件）
var checkUpdateState = 0;
function checkUpdate() {
  $(".startInfo").html("开始检测升级组件...");
  checkUpdateState = 1;

  // 检测是否安装升级程序
  var res = JSInterface.callback("startupdate", "checkupdateapp");

  // 错误
  if (res == "err") {
    $(".startInfo").html("开始检测升级组件...");
    $(".startInfo").hide();
    $(".startInfo").fadeIn();

    // 5秒后重试
    setTimeout(function () {
      checkUpdate();
    }, 5000);
    return;
  }

  // 已安装了升级程序
  if (res == "ok") {
    $(".startInfo").html("升级组件已安装...");
    checkUpdateState = 0;
  }
}

// 检测网络
var checkNetHand = null;
function checkNet() {
  // 构建日志内容
  var log = {};
  log.udid = JSInterface.callback("getudid", "");
  log.page = "index";
  log.time = Date.now();
  log.cat = 0;
  log.event = "启动页运行";
  log.message = "";
  JSInterface.callback("log", JSON.stringify(log));

  checkNetHand = setInterval(function () {
    try {
      // 先获取本机的UDID
      var udid = JSInterface.callback("getudid", "");

      // 先获取本机的版本
      var version = JSInterface.callback("getversion", "");
      $(".versionInfo").html("v" + version);

      // 检测是否可以连接服务器API
      $.ajax({
        url:
          apihost + "api/GetDeviceInfo?padNo=" +
          udid +
          "&time=" +
          Date.now(),
        type: "GET",
        success: function (data) {
          console.log(data);

          if (data.code == 200) {
            console.log(data);

            // 非版本检测状态
            if (checkUpdateState == 0) {
              $(".startInfo").html("接口检查成功...");

              clearInterval(checkNetHand);

              // 提交日志
              log.time = Date.now();
              log.cat = 0;
              log.event = "接口检查成功";
              log.message = "即将转入锁屏页";
              JSInterface.callback("log", JSON.stringify(log));

              window.location.replace(
                  apihost + "web-h5/lockpage.aspx?udid=" +
                  udid +
                  "&time=" +
                  Date.now()
              );
            }
          } else {
            $(".startInfo").html("接口检查失败...");

            // 提交日志
            log.time = Date.now();
            log.cat = -1;
            log.event = "接口检查失败";
            log.message = "接口返回：" + JSON.stringify(data);
            JSInterface.callback("log", JSON.stringify(log));
          }
        },
        error: function (err) {
          console.log(err);

          // 提交日志
          log.time = Date.now();
          log.cat = -1;
          log.event = "接口检查错误";
          log.message = "接口返回：" + JSON.stringify(err);
          JSInterface.callback("log", JSON.stringify(log));
        },
      });
    } catch (e) {
      console.log(e);
    }
  }, 5000);
}

$(document).ready(function () {

  // 获取主机
  apihost = JSInterface.callback("getapihost", "");

  // 处理键盘点击按下效果
  var elements = document.querySelectorAll(".keydiv>div");
  elements.forEach((element) => {
    element.addEventListener("touchstart", function (e) {
      if (userInputpwd.length < 6) {
        element.classList.add("keyactive");
      }
    });
  });

  // 键盘点击完毕后的数据处理
  elements.forEach((element) => {
    element.addEventListener("touchend", function (e) {
      element.classList.remove("keyactive");

      if (e.target.innerText == "清除") {
        // 去掉密码错误提示，清空现有的密码显示
        $(".pwddiv div").removeClass("pwddivErr");
        userInputpwd = "";
        setPwdShow();
        return;
      }
      if (e.target.innerText == "确定") {
        // 密码错误
        if (userInputpwd != "568120" && userInputpwd != "159357") {
          // 密码错误提示
          $(".pwddiv div").addClass("pwddivErr");
          return;
        }

        // 管理员模式
        if (userInputpwd == "159357") {
          userType = 1;
          $(".adminflag").show();
        } else {
          userType = 0;
          $(".adminflag").hide();
        }

        userInputpwd = "";
        setPwdShow();

        // 隐藏密码面板
        $("#checkPwdModal").modal("hide");

        // 去掉上次选中项 显示操作面盘
        $(".nav-link").removeClass("active");
        $("#toolsPanModal").modal("show");

        return;
      }

      // 密码还未全输入完毕 不显示错误提示
      if (userInputpwd.length < 6) {
        userInputpwd += e.target.innerText;
        // 去掉密码错误提示
        $(".pwddiv div").removeClass("pwddivErr");
      }
      console.log(userInputpwd);
      setPwdShow();
    });
  });
});

// 设置密码显示(设置和userInputpwd的长度一致)
function setPwdShow() {
  var elements = document.querySelectorAll(".pwddiv>div");
  elements.forEach((element) => {
    element.innerText = "";
  });

  for (var i = 0; i < userInputpwd.length; i++) {
    document.querySelectorAll(".pwddiv>div")[i].innerText = "*";
  }
}

// 处理功能面板点击
function call(strType) {
  // 非管理员不能进行设置操作
  if (userType == 0 && strType == "setting") {
    return;
  }

  var res = JSInterface.callback(strType, "");

  console.log(strType);
}
